package com.qyer.log;

import static com.qyer.search.Constants.SEPARATOR2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qyer.search.model.SearchParameter;
import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.utils.JsonOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * User: Z J Wu Date: 2015/08/28 Time: 22:29 Package: com.qyer.log
 */
public class ErrorLogger {

  private static final Logger LOGGER = LoggerFactory.getLogger(ErrorLogger.class);

  public static void logError(SearchParameter searchParameter, Exception e) throws
    JsonProcessingException {
    StringBuilder sb = new StringBuilder();
    sb.append(searchParameter.getRequestId());
    sb.append(SEPARATOR2);
    sb.append(searchParameter.getContext());
    sb.append(SEPARATOR2);
    sb.append(e.getClass().getSimpleName());
    sb.append(SEPARATOR2);
    sb.append(e.getMessage());
    sb.append(SEPARATOR2);

    Map<String, Object> conditions = searchParameter.getConditions();
    sb.append(JsonOperator.getInstance().toJson(conditions));
    LOGGER.error(sb.toString(), e);
  }

  public static void logError(SearchParameterOrigin searchParameter, Exception e) throws
    JsonProcessingException {
    StringBuilder sb = new StringBuilder();
    sb.append(searchParameter.getRequestId());
    sb.append(SEPARATOR2);
    sb.append(searchParameter.getContext());
    sb.append(SEPARATOR2);
    sb.append(e.getClass().getSimpleName());
    sb.append(SEPARATOR2);
    sb.append(e.getMessage());
    sb.append(SEPARATOR2);

    Map<String, Object> conditions = searchParameter.getConditions();
    sb.append(JsonOperator.getInstance().toJson(conditions));
    LOGGER.error(sb.toString(), e);
  }

  public static void logError(String requestId, String context, Map<String, Object> conditions, Exception e) throws
    JsonProcessingException {
    StringBuilder sb = new StringBuilder();
    sb.append(requestId);
    sb.append(SEPARATOR2);
    sb.append(context);
    sb.append(SEPARATOR2);
    sb.append(e.getClass().getSimpleName());
    sb.append(SEPARATOR2);
    sb.append(e.getMessage());
    sb.append(SEPARATOR2);

    sb.append(JsonOperator.getInstance().toJson(conditions));
    LOGGER.error(sb.toString(), e);
  }

}


